package com.hy.admin.security.config;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

import lombok.experimental.var;

/**
 * 说明： 自定义授权异常
 */
@Component("customWebResponseExceptionTranslator")
public class CustomWebResponseExceptionTranslator extends DefaultWebResponseExceptionTranslator{
	
	@Override
	public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
		CustomOAuth2Exception selfErrorInfo=null;
		ResponseEntity responseEntity = super.translate(e);
		OAuth2Exception body = (OAuth2Exception)responseEntity.getBody();
		HttpHeaders header = new HttpHeaders();
		        header.setAll(responseEntity.getHeaders().toSingleValueMap());

		        String errorCode = body.getOAuth2ErrorCode();
		        if (errorCode.equals(OAuth2Exception.INVALID_TOKEN)) {
		        	selfErrorInfo = new CustomOAuth2Exception("无效的Token");
		            selfErrorInfo.addAdditionalInformation("code","z001");
		        }
		        if (errorCode.equals(OAuth2Exception.INVALID_GRANT)) {
		            selfErrorInfo = new CustomOAuth2Exception("无效的密码");
		            selfErrorInfo.addAdditionalInformation("code","z002");
		        }
		        if (errorCode.equals(OAuth2Exception.INVALID_REQUEST)) {
		            selfErrorInfo = new CustomOAuth2Exception("无效的请求");
		            selfErrorInfo.addAdditionalInformation("code","z003");
		        }
		        if (errorCode.equals(OAuth2Exception.INVALID_CLIENT)) {
		            selfErrorInfo = new CustomOAuth2Exception("无效的客户端");
		            selfErrorInfo.addAdditionalInformation("code","z004");
		        }
		        if (errorCode.equals(OAuth2Exception.UNAUTHORIZED_CLIENT)) {
		            selfErrorInfo = new CustomOAuth2Exception("未经验证的客户端");
		            selfErrorInfo.addAdditionalInformation("code","z005");
		        }
		        if (errorCode.equals(OAuth2Exception.UNSUPPORTED_GRANT_TYPE)) {
		            selfErrorInfo = new CustomOAuth2Exception("不支持的密码类型");
		            selfErrorInfo.addAdditionalInformation("code","z006");
		        }
		        if (errorCode.equals(OAuth2Exception.UNSUPPORTED_RESPONSE_TYPE)) {
		            selfErrorInfo = new CustomOAuth2Exception("不支持的返回类型");
		            selfErrorInfo.addAdditionalInformation("code","z007");
		        }
		        if (errorCode.equals("unauthorized")) {
		            selfErrorInfo = new CustomOAuth2Exception(e.getMessage());
		            selfErrorInfo.addAdditionalInformation("code","z666");
		        }

		        if (selfErrorInfo == null){
		            return new ResponseEntity(body,header,responseEntity.getStatusCode());
		        }else{
		            return new ResponseEntity(selfErrorInfo,header,responseEntity.getStatusCode());
		        }
	}
	
	
//	@Override
//	public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
//		
//				CustomOAuth2Exception selfErrorInfo=null;
//				int errorCode = 401;
//				String msg = e.getMessage();
//				if(e instanceof OAuth2Exception) {
//					OAuth2Exception oAuth2Exception = (OAuth2Exception) e;
//					errorCode = oAuth2Exception.getHttpErrorCode();
//					msg=oAuth2Exception.getMessage();
//			        if (msg.equals(OAuth2Exception.INVALID_TOKEN)) {
//			            selfErrorInfo = new CustomOAuth2Exception("无效的Token");
//			        }
//			        if (msg.equals(OAuth2Exception.INVALID_GRANT)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("无效的密码");
//			        }
//			        if (msg.equals(OAuth2Exception.INVALID_REQUEST)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("无效的请求");
//			        }
//			        if (msg.equals(OAuth2Exception.INVALID_CLIENT)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("无效的客户端");
//			        }
//			        if (msg.equals(OAuth2Exception.UNAUTHORIZED_CLIENT)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("未经验证的客户端");
//			        }
//			        if (msg.equals(OAuth2Exception.UNSUPPORTED_GRANT_TYPE)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("不支持的密码类型");
//			        }
//			        if (msg.equals(OAuth2Exception.UNSUPPORTED_RESPONSE_TYPE)) {
//			        	selfErrorInfo = new CustomOAuth2Exception("不支持的返回类型");
//			        }
//			        if (msg.equals("unauthorized")) {
//			        	selfErrorInfo = new CustomOAuth2Exception(e.getMessage());
//			        }
//				}
//
//		        if (selfErrorInfo == null){
//		            return ResponseEntity.status(errorCode).body(new CustomOAuth2Exception(errorCode, msg));
//		        }else{
//		            return ResponseEntity.status(errorCode)
//		    				.body(selfErrorInfo);
//		        }
		
//	}

}
